<?php
namespace api\base;
use Yii;
use base\YiiMobo;


/**
 * Site controller
 */
class BaseApiController extends \base\BaseController
{
   //public $defaultAction = 'index2';
   
   public $layout = false;
   
   public $request = [];
   
   public $sign = '';
   
   
   public function init(){
	   parent::init();
	   $this->request = $_REQUEST;
	   
    }
	
	public function verify(){
		$request = self::getRequest();
	    $sign = $request['sign'];
		//计算允许的时间差，让timestamp 失效。
	    $timestamp['request'] = intval($request['timestamp']);
		$timestamp['max'] = time() + 600;
		$timestamp['min'] = time() - 600;
		if (!is_string($sign) || ($timestamp['request'] < $timestamp['min'] || $timestamp['request'] > $timestamp['max'])){
			
		}
		return self::encode();
		
	}
	
	public function encode(){
		$request = self::getRequest();
		return md5($request['timestamp']);
	}
   /**
   * 操作跳转类。
   * author xiongyouliang
   */
   public function verify2(){
	   $request = self::getRequest();
	   $sign = $request['sign'];
	   //$timestamp = $request['timestamp'];
	   //$ino = $request['ino'];
	   $private_key = file_get_contents(dirname(__FILE__).'/../key/rsa_private_key.pem');
        if (empty($private_key))
        {
            echo "Private Key error!";
            return false;
        }

        $pkeyid = openssl_get_privatekey($private_key);
        if (empty($pkeyid))
        {
            echo "private key resource identifier False!";
            return false;
        }

        $verify = openssl_sign($sign, $signature, $pkeyid, OPENSSL_ALGO_MD5);
        openssl_free_key($pkeyid);
		exit($signature);
        return $signature;
	   
   }
   
   
   
   /**
     * 利用公钥和数字签名以及约定数据验证合法性
     * @param $data 待验证数据
     * @param $signature 数字签名
     * @return -1:error验证错误 1:correct验证成功 0:incorrect验证失败
     */
    public function isValid($data='', $signature='')
    {
        if (empty($data) || empty($signature))
        {
            return false;
        }

        $public_key = '-----BEGIN PUBLIC KEY-----  
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3//sR2tXw0wrC2DySx8vNGlqt  
3Y7ldU9+LBLI6e1KS5lfc5jlTGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2kl  
Bd6h4wrbbHA2XE1sq21ykja/Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o  
2n1vP1D+tD3amHsK7QIDAQAB  
-----END PUBLIC KEY-----';
        if (empty($public_key))
        {
            echo "Public Key error!";
            return false;
        }

        $pkeyid = openssl_get_publickey($public_key);
        if (empty($pkeyid))
        {
            echo "public key resource identifier False!";
            return false;
        }

        $ret = openssl_verify($data, $signature, $pkeyid, OPENSSL_ALGO_MD5);
        switch ($ret)
        {
            case -1:
                echo "error";
                break;
            default:
                echo $ret==1 ? "correct" : "incorrect";//0:incorrect
                break;
        }
        return $ret;
    }

   
   /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
	
	public function getRequest(){
	    if(!$this->request){
		    $this->request = $_REQUEST;
		}	
		return $this->request;	
	}
}
